#include "fontconfigdialog.h"
#include "drawproperty.h"
#include "colorpanel.h"
#include "lineconfigdialog.h"

#include <QPainter>
#include <QHBoxLayout>
#include <QDebug>

//QList<CDrawProperty*> CDrawProperty::groupPropTypeList;
CDrawProperty* CDrawProperty::m_pCurrentDrawProperty = nullptr;

CDrawProperty::CDrawProperty(QWidget *p, drawType type, QWidget *parent)
    : QWidget(parent)
    , m_type(type)
    , m_p(p)
    , m_showProperty(false)
    , m_property()
    , m_nCursor(Qt::ArrowCursor)
{
    //groupPropTypeList.append(this);
    Init();
}

CDrawProperty::~CDrawProperty()
{
    if (m_pCurrentDrawProperty == this)
    {
        m_pCurrentDrawProperty = nullptr;
    }
}

void CDrawProperty::SetCurrentPropertyPtr(CDrawProperty *ptr)
{
    m_pCurrentDrawProperty = ptr;
}

CDrawProperty *CDrawProperty::GetCurrentPropertyPtr()
{
    return m_pCurrentDrawProperty;
}

bool CDrawProperty::ShowCurrentWindow()
{
    if (m_pCurrentDrawProperty)
    {
        m_pCurrentDrawProperty->show();
        return true;
    }

    return false;
}

bool CDrawProperty::HideCurrentWindow()
{
    if (m_pCurrentDrawProperty)
    {
        m_pCurrentDrawProperty->hide();
        return true;
    }

    return false;
}

void CDrawProperty::ShowWidgetAndHideOtherGroupWidget()
{
//    QList<CDrawProperty*>::const_iterator iter = groupPropTypeList.begin();
//    for (; groupPropTypeList.end() != iter; ++iter)
//    {
//        if (*iter)
//        {
//            if (this == *iter)
//            {
//                this->show();
//            }
//            else
//            {
//                (*iter)->hide();
//            }
//        }
    //    }
}

void CDrawProperty::SetProperty(const DRAWPROPERTY &property)
{
    m_property = property;
}

void CDrawProperty::PropertyColorSel(const QColor &color)
{
    m_property.penColor = color;
    emit PropertyChange(m_property);
}

void CDrawProperty::PropertyWidth(int nWidth)
{
    if (nWidth <= 0)
    {
        return;
    }
    m_property.penWidth = nWidth;
    emit PropertyChange(m_property);
}

void CDrawProperty::PropertyFont(const QFont &font)
{
    m_property.font = font;
    emit PropertyChange(m_property);
}

void CDrawProperty::mousePressEvent(QMouseEvent *event)
{
    Q_UNUSED(event)
}

void CDrawProperty::enterEvent(QEvent *event)
{
    QWidget::enterEvent(event);

    m_nCursor = cursor().shape();
    setCursor(Qt::ArrowCursor);
}

void CDrawProperty::leaveEvent(QEvent *event)
{
    QWidget::leaveEvent(event);

    setCursor(m_nCursor);
}

void CDrawProperty::showEvent(QShowEvent *event)
{
    QWidget::showEvent(event);
    if (m_pCurrentDrawProperty != this)
    {
        return;
    }
    if (m_p)
    {
        int height = m_p->sizeHint().height();
        move(m_p->mapToParent(QPoint(0, height+2)));
        //resize(m_p->sizeHint().width(), m_p->sizeHint().height());
        PropertyChange(m_property);
    }
}

void CDrawProperty::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QBrush(QColor(243, 243, 243)));
    painter.drawRoundedRect(QWidget::rect(), 10, 10);
}

void CDrawProperty::CreateWindow(CDrawProperty::drawType type)
{
    switch (type)
    {
    case Line:
        CreateLinePropertyWindow();
        break;
    case rect:
        CreateRectPropertyWindow();
        break;
    case text:
        CreateTextPropertyWindow();
        break;
    default:
        break;
    }
}

void CDrawProperty::CreateLinePropertyWindow()
{
    setContentsMargins(0, 0, 0, 0);
    QHBoxLayout *pHboxLayout = new QHBoxLayout(this);
    pHboxLayout->setContentsMargins(0, 0, 0, 0);
    //线型

    //线宽

    //线条颜色
    CColorPanel *pColorPanel = new CColorPanel(this);
    pHboxLayout->addWidget(pColorPanel);
    connect(pColorPanel, &CColorPanel::ColorSel, this, &CDrawProperty::PropertyColorSel);
    CLineConfigDialog *pLineConfig = new CLineConfigDialog(this);
    pLineConfig->SetPenWidth(m_property.penWidth);
    pHboxLayout->addWidget(pLineConfig);
    connect(pLineConfig, &CLineConfigDialog::PenWidthChanged, this, &CDrawProperty::PropertyWidth);
}

void CDrawProperty::CreateRectPropertyWindow()
{
    QHBoxLayout *pHboxLayout = new QHBoxLayout(this);
    pHboxLayout->setContentsMargins(0, 0, 0, 0);
    //线型

    //线宽

    //线条颜色
    CColorPanel *pColorPanel = new CColorPanel(this);
    pHboxLayout->addWidget(pColorPanel);
    connect(pColorPanel, &CColorPanel::ColorSel, this, &CDrawProperty::PropertyColorSel);
}

void CDrawProperty::CreateTextPropertyWindow()
{
    QHBoxLayout *pHboxLayout = new QHBoxLayout(this);
    pHboxLayout->setObjectName("pHboxLayout");
    pHboxLayout->setContentsMargins(0, 0, 0, 0);
    //线型

    //线宽

    //线条颜色
    CColorPanel *pColorPanel = new CColorPanel(this);
    pHboxLayout->addWidget(pColorPanel);
    connect(pColorPanel, &CColorPanel::ColorSel, this, &CDrawProperty::PropertyColorSel);

    CFontConfigDialog *pFontConfigDialog = new CFontConfigDialog(this);
    pHboxLayout->addWidget(pFontConfigDialog);
    pFontConfigDialog->SetFont(m_property.font);
    connect(pFontConfigDialog, &CFontConfigDialog::FontChanged, this, &CDrawProperty::PropertyFont);
}

void CDrawProperty::Init()
{
//    setWindowFlag(Qt::WindowStaysOnTopHint);
//    setWindowFlag(Qt::FramelessWindowHint);
    setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint /*| Qt::Tool*/);
    setAttribute(Qt::WA_QuitOnClose, false);
    CreateWindow(m_type);
    hide();
    m_property.penColor = QColor(255, 0, 0);
    m_property.penWidth = 1;
    setMaximumHeight(60);
}
